package yxy.game.pm2.command.godBeast;

import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.therion;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.PickedGodBeastMessage;
import yxy.game.pm2.module.CurrencyHandle;
import yxy.game.pm2.module.battle.BattleHandle;
import yxy.game.pm2.module.conquest.ConquestModule;
import yxy.game.pm2.module.godBeast.GodBeast;
import yxy.game.pm2.module.godBeast.PickedGodBeastHandle;
import yxy.game.pm2.module.godBeast.PickedGodBeastModule;
import yxy.game.pm2.module.playtype.PlayTypeHandle;
import yxy.game.pm2.module.playtype.PlayTypeModule;

import java.util.Collection;

/**
 * @author wrs
 * @name CMD_PickedGodBeastUnlock
 * @descrition 神兽槽解锁
 * @since 2022-05-31
 */
@MessageType(proto = Command.PickedGodBeastUnlock, classtype = PickedGodBeastMessage.PickedGodBeastUnlockRequest.class)
public class CMD_PickedGodBeastUnlock extends Command<PickedGodBeastMessage.PickedGodBeastUnlockRequest> {

    @Override
    public void execute(MessageSource messageSource, PickedGodBeastMessage.PickedGodBeastUnlockRequest request) throws Exception {
        String playerCode = messageSource.getTag();
        int code = request.getCode(); //神兽ID

        PickedGodBeastMessage.PickedGodBeastUnlockResponse.Builder response = PickedGodBeastMessage.PickedGodBeastUnlockResponse.newBuilder();

        try {
            PickedGodBeastModule pgbModule = PickedGodBeastModule.get(playerCode);
            if (pgbModule == null) {
                send(messageSource, response, Result.DataError);//不存在参数错误
                return;
            }

            ConquestModule module = ConquestModule.get(playerCode);
            if (module.getMapId() < PickedGodBeastModule.getData().getMapnum()) {
                send(messageSource, response, Result.DataError);//关卡限制
                return;
            }

            GodBeast godBeast = new PickedGodBeastHandle().getGodBeastById(pgbModule, code);
            if (godBeast == null) {
                send(messageSource, response, Result.DataError);//
                return;
            }

            if (godBeast.isOpen()) {
                send(messageSource, response, Result.GodBeastIsActivated);//神兽已激活
                return;
            }

            //背包中的神兽令是否足够
            therion therion = DataCenter.getData(godBeast.getCode(), yxy.cherry.data.bean.therion.class);
            Collection<Currency> currencies = Currency.create(therion.getCosttype(), therion.getCostid(), therion.getCostnum());
            if (!new CurrencyHandle().costCheck(playerCode, currencies)) {
                send(messageSource, response, Result.CostFail);//消费不足
                return;
            }

            //激活神兽
            new PickedGodBeastHandle().activateGodBeast(playerCode, godBeast, response);

            send(messageSource, response, Result.Success);

            PlayTypeModule playTypeModule = PlayTypeModule.get(playerCode);
            PlayTypeHandle.savePickedGodBeast(playerCode, playTypeModule);
            new BattleHandle().powerUpdate(playerCode);// 战力更新

        } catch (Exception e) {
            e.printStackTrace();
            send(messageSource, response, Result.DataError); //数据错误
        }
    }

    private void send(MessageSource messageSource, PickedGodBeastMessage.PickedGodBeastUnlockResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }
}
